nanopyx.liquid._le_interpolation_catmull_rom_
1import numpy as np 2from math import floor 3from .__njit__ import njit, prange 4 5 6def _cubic(v): 7 a = 0.5 8 z = 0 9 if v < 0: 10 v = -v 11 12 if v < 1: 13 z = v * v * (v * (-a + 2) + (a - 3)) + 1 14 elif v < 2: 15 z = -a * v * v * v + 5 * a * v * v - 8 * a * v + 4 * a 16 17 return z 18 19@njit(cache=True) 20def _njit_cubic(v): 21 a = 0.5 22 z = 0 23 if v < 0: 24 v = -v 25 26 if v < 1: 27 z = v * v * (v * (-a + 2) + (a - 3)) + 1 28 elif v < 2: 29 z = -a * v * v * v + 5 * a * v * v - 8 * a * v + 4 * a 30 31 return z 32 33def _interpolate(image, r, c, rows, cols): 34 if r < 0 or r >= rows or c < 0 or c >= cols: 35 return 0 36 r_int = int(floor(r - 0.5)) 37 c_int = int(floor(c - 0.5)) 38 q = 0 39 p = 0 40 41 for j in range(4): 42 c_neighbor = c_int - 1 + j 43 p = 0 44 if c_neighbor < 0 or c_neighbor >= cols: 45 continue 46 47 for i in range(4): 48 r_neighbor = r_int - 1 + i 49 if r_neighbor < 0 or r_neighbor >= rows: 50 continue 51 52 p = p + image[r_neighbor,c_neighbor] * _cubic(r - (r_neighbor + 0.5)) 53 q = q + p * _cubic(c - (c_neighbor + 0.5)) 54 55 return q 56 57 58@njit(cache=True) 59def _njit_interpolate(image, r, c, rows, cols): 60 if r < 0 or r >= rows or c < 0 or c >= cols: 61 return 0 62 r_int = int(floor(r - 0.5)) 63 c_int = int(floor(c - 0.5)) 64 q = 0 65 p = 0 66 67 for j in range(4): 68 c_neighbor = c_int - 1 + j 69 p = 0 70 if c_neighbor < 0 or c_neighbor >= cols: 71 continue 72 73 for i in range(4): 74 r_neighbor = r_int - 1 + i 75 if r_neighbor < 0 or r_neighbor >= rows: 76 continue 77 78 p = p + image[r_neighbor, c_neighbor] * _njit_cubic(r - (r_neighbor + 0.5)) 79 q = q + p * _njit_cubic(c - (c_neighbor + 0.5)) 80 81 return q 82 83 84def shift_magnify( 85 image: np.ndarray, 86 shift_row: np.ndarray, 87 shift_col: np.ndarray, 88 magnification_row: float, 89 magnification_col: float, 90) -> np.ndarray: 91 """ 92 Shift and magnify using nearest neighbor interpolation. 93 :param image: 3D numpy array to interpolate with size (nFrames, nRow, nCol) 94 :param shift_row: 1D array with size (nFrames) with values to shift the rows 95 :param shift_col: 1D array with size (nFrames) with values to shift the cols 96 :param magnification_row: float magnification factor for the rows 97 :param magnification_col: float magnification factor for the cols 98 :return: 3D float32 numpy array with the result 99 """ 100 101 nFrames = image.shape[0] 102 rows = image.shape[1] 103 cols = image.shape[2] 104 rowsM = int(rows * magnification_row) 105 colsM = int(cols * magnification_col) 106 107 image_out = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) 108 for f in range(nFrames): 109 for j in range(colsM): 110 col = j / magnification_col - shift_col[f] 111 for i in range(rowsM): 112 row = i / magnification_row - shift_row[f] 113 image_out[f, i, j] = _interpolate(image[f, :, :], row, col, rows, cols) 114 115 return image_out 116 117 118@njit(cache=True, parallel=True) 119def njit_shift_magnify( 120 image: np.ndarray, 121 shift_row: np.ndarray, 122 shift_col: np.ndarray, 123 magnification_row: float, 124 magnification_col: float, 125) -> np.ndarray: 126 """ 127 Shift and magnify using nearest neighbor interpolation. 128 :param image: 3D numpy array to interpolate with size (nFrames, nRow, nCol) 129 :param shift_row: 1D array with size (nFrames) with values to shift the rows 130 :param shift_col: 1D array with size (nFrames) with values to shift the cols 131 :param magnification_row: float magnification factor for the rows 132 :param magnification_col: float magnification factor for the cols 133 :return: 3D float32 numpy array with the result 134 """ 135 136 nFrames = image.shape[0] 137 rows = image.shape[1] 138 cols = image.shape[2] 139 rowsM = int(rows * magnification_row) 140 colsM = int(cols * magnification_col) 141 142 image_out = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) 143 for f in range(nFrames): 144 for j in prange(colsM): 145 col = j / magnification_col - shift_col[f] 146 for i in range(rowsM): 147 row = i / magnification_row - shift_row[f] 148 image_out[f, i, j] = _njit_interpolate(image[f, :, :], row, col, rows, cols) 149 150 return image_out
def
shift_magnify( image: numpy.ndarray, shift_row: numpy.ndarray, shift_col: numpy.ndarray, magnification_row: float, magnification_col: float) -> numpy.ndarray:
85def shift_magnify( 86 image: np.ndarray, 87 shift_row: np.ndarray, 88 shift_col: np.ndarray, 89 magnification_row: float, 90 magnification_col: float, 91) -> np.ndarray: 92 """ 93 Shift and magnify using nearest neighbor interpolation. 94 :param image: 3D numpy array to interpolate with size (nFrames, nRow, nCol) 95 :param shift_row: 1D array with size (nFrames) with values to shift the rows 96 :param shift_col: 1D array with size (nFrames) with values to shift the cols 97 :param magnification_row: float magnification factor for the rows 98 :param magnification_col: float magnification factor for the cols 99 :return: 3D float32 numpy array with the result 100 """ 101 102 nFrames = image.shape[0] 103 rows = image.shape[1] 104 cols = image.shape[2] 105 rowsM = int(rows * magnification_row) 106 colsM = int(cols * magnification_col) 107 108 image_out = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) 109 for f in range(nFrames): 110 for j in range(colsM): 111 col = j / magnification_col - shift_col[f] 112 for i in range(rowsM): 113 row = i / magnification_row - shift_row[f] 114 image_out[f, i, j] = _interpolate(image[f, :, :], row, col, rows, cols) 115 116 return image_out
Shift and magnify using nearest neighbor interpolation.
Parameters
- image: 3D numpy array to interpolate with size (nFrames, nRow, nCol)
- shift_row: 1D array with size (nFrames) with values to shift the rows
- shift_col: 1D array with size (nFrames) with values to shift the cols
- magnification_row: float magnification factor for the rows
- magnification_col: float magnification factor for the cols
Returns
3D float32 numpy array with the result
@njit(cache=True, parallel=True)
def
njit_shift_magnify( image: numpy.ndarray, shift_row: numpy.ndarray, shift_col: numpy.ndarray, magnification_row: float, magnification_col: float) -> numpy.ndarray:
119@njit(cache=True, parallel=True) 120def njit_shift_magnify( 121 image: np.ndarray, 122 shift_row: np.ndarray, 123 shift_col: np.ndarray, 124 magnification_row: float, 125 magnification_col: float, 126) -> np.ndarray: 127 """ 128 Shift and magnify using nearest neighbor interpolation. 129 :param image: 3D numpy array to interpolate with size (nFrames, nRow, nCol) 130 :param shift_row: 1D array with size (nFrames) with values to shift the rows 131 :param shift_col: 1D array with size (nFrames) with values to shift the cols 132 :param magnification_row: float magnification factor for the rows 133 :param magnification_col: float magnification factor for the cols 134 :return: 3D float32 numpy array with the result 135 """ 136 137 nFrames = image.shape[0] 138 rows = image.shape[1] 139 cols = image.shape[2] 140 rowsM = int(rows * magnification_row) 141 colsM = int(cols * magnification_col) 142 143 image_out = np.zeros((nFrames, rowsM, colsM), dtype=np.float32) 144 for f in range(nFrames): 145 for j in prange(colsM): 146 col = j / magnification_col - shift_col[f] 147 for i in range(rowsM): 148 row = i / magnification_row - shift_row[f] 149 image_out[f, i, j] = _njit_interpolate(image[f, :, :], row, col, rows, cols) 150 151 return image_out
Shift and magnify using nearest neighbor interpolation.
Parameters
- image: 3D numpy array to interpolate with size (nFrames, nRow, nCol)
- shift_row: 1D array with size (nFrames) with values to shift the rows
- shift_col: 1D array with size (nFrames) with values to shift the cols
- magnification_row: float magnification factor for the rows
- magnification_col: float magnification factor for the cols
Returns
3D float32 numpy array with the result